<template>
  <div class="upload-img" @mouseover="show = true" @mouseleave="show = false">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img v-if="value" v-imgError :src="value" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- 删除按钮 -->
    <transition name="big">
      <i v-if="value && show" class="del el-icon-close" @click="del" />
    </transition>
    <!-- 预览按钮 -->
    <transition name="big">
      <i
        v-if="value && show"
        class="big-img el-icon-zoom-in"
        @click="showBig = true"
      />
    </transition>
    <!-- 大图片弹框 -->
    <el-dialog title="预览" :visible.sync="showBig" width="800px">
      <img v-imgError style="width: 100%" :src="value" alt="">
    </el-dialog>
    <!-- 进度条 -->
    <el-progress
      v-if="showProgress"
      color="red"
      class="progress"
      type="circle"
      :percentage="percentage"
    />
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  SecretId: 'AKIDNXR8bpeSF1JL8EyZopxRfdHgqGUeuEn8',
  SecretKey: 'HJ9mAbQEn7jgjY9swEI6yAamk7MN8hty'
})
export default {
  name: 'UploadImg',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      show: false,
      showBig: false,
      showProgress: false,
      percentage: 0
      //   imageUrl: ''
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    httpRequest(res) {
      console.log(res)
      this.showProgress = true
      this.$emit('input', '')
      cos.putObject(
        {
          Bucket: 'abc-1305249343' /* 必须:存储桶 */,
          Region: 'ap-chongqing' /* 存储桶所在地域，必须字段 */,
          Key: '/hm86/' + res.file.name /* 必须:名字，要求一定是字符串 */,
          StorageClass: 'STANDARD', // 固定配制
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            // 上传的进度值
            this.percentage = +(progressData.percent * 100).toFixed(2)
            if (progressData.percent === 1) {
              this.showProgress = false
            }
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          // 上传结束
          // err有值就是失败了，没值才是成功
          // 成功才有data,data中有上传完成的接口返回值
          //   this.imageUrl = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
          console.log(err || data)
        }
      )
    },
    // 删除事件
    del() {
      this.$emit('input', '')
    }
  }
}
</script>
<style lang="scss" scoped>
// scoped:只管自己当前组件和子组件最外层
.avatar-uploader ::v-deep .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader ::v-deep .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.upload-img {
  position: relative;
  width: 178px;
  &:hover {
    .del {
      //   display: block;
    }
    .big-img {
      //   display: block;
    }
  }
  .del {
    position: absolute;
    right: 5px;
    top: 5px;
    font-size: 40px;
    cursor: pointer;
    // display: none;
  }
  .big-img {
    position: absolute;
    top: 5px;
    right: 50px;
    font-size: 40px;
    cursor: pointer;
    // display: none;
  }
}
.big-enter {
  transform: scale(0.1);
  opacity: 0;
}
.big-leave-to {
  opacity: 0;
  transform: scale(0.1);
}
.big-enter-active,
.big-leave-active {
  transition: all 0.5s;
}
.progress {
  position: absolute;
  top: 0;
  left: 0;
  background-color: #fff;
  ::v-deep .el-progress-circle {
    width: 178px !important;
    height: 178px !important;
  }
}
</style>
